﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Linq;

namespace NCAABasketballTournament
{
    public class Bracket
    {
        public string Name { get; set; }

        public List<Team> Round1 = new List<Team>(); //16
        public List<Team> Round2 = new List<Team>(); //8
        public List<Team> Round3 = new List<Team>(); //4
        public List<Team> Round4 = new List<Team>(); //2
        public List<Team> Round5 = new List<Team>(); //1

        public Team FindTeamWithSeed(int iSeed)
        {
            var oFound = (from oItem in Round1
                        where oItem.Seed == iSeed
                        select oItem).ToList().FirstOrDefault<Team>();

            return oFound;
        }

        public Team RunBracket(List<Team> oTeams)
        {
            int[] iMap = { 1, 16, 8, 9, 5, 12, 4, 13, 6, 11, 3, 14, 7, 10, 2, 15 };
            var oResult = FillRound1(oTeams, iMap);

            oResult = Advance(oResult,Round2);
            oResult = Advance(oResult,Round3);
            oResult = Advance(oResult,Round4);
            oResult = Advance(oResult,Round5);
            if ( oResult.Count == 1 )
                return oResult[0];

            return new Team() { TeamName = "Failure" };
        }

        public Team RunFinals(List<Team> oTeams)
        {
            Round1 = oTeams;

            var oResult = Advance(Round1, Round2);
            oResult = Advance(oResult, Round3);
            if (oResult.Count == 1)
                return oResult[0];

            return new Team() { TeamName = "Failure" };
        }

        public List<Team> Advance(List<Team> oRoundLast, List<Team> oRoundNext)
        {
            //move teams into next round based on "Score"

            int iTotal = oRoundLast.Count;
            int iEven = iTotal % 2;
            if (iEven != 0)
                return oRoundNext;

            //these are ordered by seed, now determine who plays whom
            for (int i = 0; i < iTotal; i += 2)
            {
               Team oFirst = oRoundLast[i];
               Team oSecond = oRoundLast[i+1];

               Team oPick = oFirst.Score >= oSecond.Score ? oFirst : oSecond;

               oRoundNext.Add(oPick);
            }

            return oRoundNext;
        }
 
        public List<Team> FillRound1(List<Team> oTeams, int[] oMapping)
        {
            //extra linq statement to validate input teams
            var oList = (from oTeam in oTeams
                         where Name.StartsWith(oTeam.Region)
                         orderby oTeam.Seed
                         select oTeam).ToList<Team>();

            int iTotal = oList.Count;

            //these are ordered by seed, now determine who plays whom
            for (int i = 0; i < iTotal; i++)
            {
                int iPosition = oMapping[i] - 1;
                var oTeam = oList[iPosition];
                Round1.Add(oTeam);
            }

            return Round1;
        }


    }
}
